package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.entity.WaWasteType;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.common.entity.OrgWaste;
import com.sharemarking.wa.platform.dto.waste.OrgWasteInfoDto;
import com.sharemarking.wa.platform.dto.waste.OrgWasteListDto;
import com.sharemarking.wa.platform.dto.wasteType.WaWasteTypeDto;
import com.sharemarking.wa.platform.dto.wasteType.WasteTypeListDto;
import com.sharemarking.wa.platform.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class ManageWasteService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgWasteMapper orgWasteMapper;
    @Autowired
    private WasteTypeMapper wasteTypeMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private OrgMemberMapper OrgMemberMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private WasteTypeInfoMapper wasteTypeInfoMapper;
    public ResponseParams<?> getInfo() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String id = request.getParameter("id");

        if (StringUtils.isEmpty(id)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("id"));
        }

        OrgWaste orgWaste = orgWasteMapper.selectById(Integer.valueOf(id));
        OrgWasteInfoDto wasteInfoDto = new OrgWasteInfoDto();
        if (orgWaste != null) {
            BeanUtils.copyProperties(orgWaste, wasteInfoDto);
        }

        return ResponseParams.ok(wasteInfoDto);
    }

    public ResponseParams<?> getWasteTypeList() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String searchStr = request.getParameter("searchStr");

        List<WasteTypeListDto> wasteType;
        if (StringUtils.isEmpty(searchStr)) {
            wasteType = wasteTypeMapper.getWasteTypeList();
        } else {
            wasteType = wasteTypeMapper.getWasteTypeListByName(searchStr);
        }

        return ResponseParams.ok(wasteType);

    }

    public ResponseParams<?> getList() throws Exception {
        Integer memberId = ss.getCurrentUserId();

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);

        List<OrgWasteListDto> list = orgWasteMapper.getOrgWasteList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, orgWasteMapper.getOrgWasteTotal(params), list.size() == pageSize));
    }

    public ResponseParams<?> addOrEdit() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String pic = request.getParameter("pic");
        String wasteTypeId = request.getParameter("wasteTypeId");
        String characteristics = request.getParameter("characteristics");
        String form = request.getParameter("form");
        //String transferPurpose = request.getParameter("transferPurpose");
        String packageType = request.getParameter("packageType");
        String dangerousIngredients = request.getParameter("dangerousIngredients");
        String measure = request.getParameter("measure");

        if (StringUtils.isEmpty(name)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("name"));
        }
        if (StringUtils.isEmpty(wasteTypeId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("wasteTypeId"));
        }


        OrgWaste orgWaste = new OrgWaste();
        orgWaste.setName(name);
        orgWaste.setCharacteristics(characteristics);
        orgWaste.setDangerousIngredients(dangerousIngredients);
        orgWaste.setForm(form);
        orgWaste.setMeasure(measure);
        orgWaste.setPackageType(packageType);
        orgWaste.setWasteTypeId(Integer.valueOf(wasteTypeId));
        orgWaste.setWasteTypeName(wasteTypeMapper.selectById(wasteTypeId).getName());
        orgWaste.setPic(pic);
        //orgWaste.setTransferPurpose(transferPurpose);

        //获取orgId
        OrgMember org = OrgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", memberId));
        orgWaste.setOrgId(org.getOrgId());

        if (StringUtils.isEmpty(id)) {
            //插入
            orgWaste.setCreateDate(new Date());
            orgWasteMapper.insert(orgWaste);
        } else {
            //更新
            orgWaste.setId(Integer.valueOf(id));
            orgWaste.setUpdateDate(new Date());
            orgWasteMapper.updateById(orgWaste);
        }
        return ResponseParams.ok(null);
    }


    public ResponseParams<?> getWasteForm()throws Exception{
        Integer memberId = ss.getCurrentUserId();
        List<String> formList = dictMapper.getDictLabelByType("waste_form");
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",formList);
        return ResponseParams.ok(map);
    }

    public ResponseParams<?> delete() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("id"));
        }

        Integer orgId = OrgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id",memberId)).getOrgId();
        if (orgId.equals(orgWasteMapper.selectById(id).getOrgId())){
            orgWasteMapper.deleteById(id);
        }else {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }

        return ResponseParams.ok(null);
    }

    public ResponseParams<?> getWasteTypeInfo() throws RequestException {
        Integer memberId = ss.getCurrentUserId();
        String id = request.getParameter("id");
        if (memberMapper.getRoleByMemberId(memberId) != 0) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }
        WaWasteType wastType = wasteTypeInfoMapper.selectById(Integer.parseInt(id));
        WaWasteTypeDto waWasteTypeDto = new WaWasteTypeDto();
        BeanUtils.copyProperties(wastType,waWasteTypeDto);
        return ResponseParams.ok(waWasteTypeDto);


    }
}
